package com.frcdi.qms.web.controller.equipment;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.equipment.domain.EquipmentBase;
import com.frcdi.qms.equipment.domain.EquipmentCycle;
import com.frcdi.qms.equipment.service.IEquipmentBaseService;
import com.frcdi.qms.equipment.service.IEquipmentCycleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备基础信息Controller
 *
 * @author cell
 * @date 2023-12-07
 */
@Api(tags = "设备基础信息")
@RestController
@RequestMapping("/equipment/equipmentBase")
public class EquipmentBaseController extends BaseController
{
    @Autowired
    private IEquipmentBaseService equipmentBaseService;

    @Autowired
    private IEquipmentCycleService cycleService;

    /**
     * 查询设备基础信息列表
     */
    @ApiOperation("查询设备基础信息列表")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentBase equipmentBase)
    {
        startPage();
        List<EquipmentBase> list = equipmentBaseService.selectEquipmentBaseList(equipmentBase);
        return getDataTable(list);
    }

    /**
     * 导出设备基础信息列表
     */
    @ApiOperation("导出设备基础信息列表")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentBase equipmentBase)
    {
        List<EquipmentBase> list = equipmentBaseService.selectEquipmentBaseList(equipmentBase);
        ExcelUtil<EquipmentBase> util = new ExcelUtil<EquipmentBase>(EquipmentBase.class);
        util.exportExcel(response, list, "设备基础信息数据");
    }

    /**
     * 获取设备基础信息详细信息
     */
    @ApiOperation("获取设备基础信息详细信息")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:query')or @ss.hasPermi('workflow:flowable:list')or @ss.hasPermi('masterData:execution:list')")
    @GetMapping(value = "/{equipmentId}")
    public AjaxResult getInfo(@PathVariable("equipmentId") Long equipmentId)
    {
        EquipmentBase equipmentBase = equipmentBaseService.selectEquipmentBaseByEquipmentId(equipmentId);

        AjaxResult ajax = AjaxResult.success();
        ajax.put(AjaxResult.DATA_TAG, equipmentBase);
        ajax.put("equipmentCycle", equipmentBase.getCycles().stream().map(EquipmentCycle::getCycleStatus).collect(Collectors.toList()));
        return ajax;
    }

    /**
     * 新增设备基础信息
     */
    @ApiOperation("新增设备基础信息")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:add')")
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentBase equipmentBase)
    {
        if (!equipmentBaseService.checkequipmentBaseNameUnique(equipmentBase)){
            return error(equipmentBase + "'失败，名称已存在");
        }
        equipmentBase.setCreateBy(getUsername());
        return success(equipmentBaseService.insertEquipmentBase(equipmentBase));
    }

    /**
     * 修改设备基础信息
     */
    @ApiOperation("修改设备基础信息")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentBase equipmentBase)
    {
        if (!equipmentBaseService.checkequipmentBaseNameUnique(equipmentBase)){
        return error(equipmentBase + "'失败，名称已存在");
        }
        equipmentBase.setUpdateBy(getUsername());
        // 编辑时重置所有状态
        equipmentBase.setStatus(UserConstants.DISABLE);
        equipmentBase.setAuditResult(UserConstants.AUDIT_NOT_AUDITED);
        equipmentBase.setSend(UserConstants.SEND_NOT_RETURNED);
        equipmentBase.setTaskStatus(UserConstants.TASK_NOT);
        return toAjax(equipmentBaseService.updateEquipmentBase(equipmentBase));
    }

    /**
     * 修改流程编号
     */
    @ApiOperation("修改流程编号")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:edit')")
    @PutMapping("/equipment")
    public AjaxResult editById(@RequestBody EquipmentBase equipmentBase)
    {
        equipmentBase.setUpdateBy(getUsername());
        return toAjax(equipmentBaseService.updateEquipmentBase(equipmentBase));
    }

    /**
     * 删除设备基础信息
     */
    @ApiOperation("删除设备基础信息")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:remove')")
	@DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@PathVariable Long[] equipmentIds)
    {
        return toAjax(equipmentBaseService.deleteEquipmentBaseByEquipmentIds(equipmentIds));
    }

    /**
     * 提交至申请人
     */
    @ApiOperation("提交至申请人")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:add')or @ss.hasPermi('equipment:equipmentBase:edit')")
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody EquipmentBase equipmentBase)
    {
        return toAjax(equipmentBaseService.submitApplicant(equipmentBase));
    }
}
